package com.example.xunpuusbprintdemo3;


import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.siupo.usbdriver.UsbPrinter;
import com.siupo.usbdriver.UsbPrinter.ALIGN;
import com.siupo.usbdriver.UsbPrinter.BAR_TYPE_1D;
import com.siupo.usbdriver.UsbPrinter.CUT_MODE;
import com.siupo.usbdriver.UsbPrinter.FEED_DIR;
import com.siupo.usbdriver.UsbPrinter.FONT_BOLD;
import com.siupo.usbdriver.UsbPrinter.FONT_ROTATE;
import com.siupo.usbdriver.UsbPrinter.FONT_UNDERLINE;
import com.siupo.usbdriver.UsbPrinter.HRI;
import com.siupo.usbdriver.UsbPrinter.OnStatusListener;
import com.siupo.usbdriver.UsbPrinter.PRINT_DIR;
import com.siupo.usbdriver.UsbPrinter.REVERSE_VIDEO;
import com.siupo.usbdriver.UsbPrinter.TYPE_CHAR;
import com.siupo.usbdriver.UsbPrinter.TYPE_SPACE;
import com.siupo.usbdriver.UsbPrinter.TYPE_STATUS;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private UsbPrinter usbPrinter;//全局app
    private int dirSelected;
    private int bRotate = 0;

    TextView tv_Sta;

    TextView tSta;
    TextView tId;
    Timer tmGetSta = new Timer();
    //定时更新打印机状态
    TimerTask tTaskGetSta = new TimerTask() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    if (usbPrinter.isConnected()) {
                        int len = usbPrinter.GetPortStatus();
                        if (len == 0x18) {
                            tSta.setText("打印机状态：正常");
                        } else if (len == 0x38) {
                            tSta.setText("打印机状态：缺纸");
                        } else if (len == 0x10) {
                            tSta.setText("打印机状态：有纸，异常"); //异常包括抬杆，切刀等
                        } else if (len == 0x30) {
                            tSta.setText("打印机状态：缺纸，异常"); //异常包括抬杆，切刀等
                        } else {
                            tSta.setText("打印机状态：" + String.format("0x%X", len));
                        }
                        long id = usbPrinter.GetSerialNum();
                        tId.setText("打印机ID：" + String.format("0x%X", id));

                        len = usbPrinter.POS_GetPrinterStatus(TYPE_STATUS._PAPER);
                        tv_Sta.setText("实时状态：" + String.format("0x%X", len));

                    } else {
                        tSta.setText("打印机状态：未连接");
                        tId.setText("打印机ID：------");
                        tv_Sta.setText("实时状态：未连接");
                    }
                }
            });
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tSta = (TextView) findViewById(R.id.textViewSta);
        tId = (TextView) findViewById(R.id.textViewID);
        tv_Sta = (TextView) findViewById(R.id.tv_status);

       // tmGetSta.schedule(tTaskGetSta, 2000, 1000);

        // 打印机连接状态
        UsbPrinter.OnConnectListener mOnConnectListener = new UsbPrinter.OnConnectListener() {
            @Override
            public void onConnect(int cnnt) {
                Log.i(TAG, "onConnect: 连接，cnnt = " + cnnt);
                // TODO Auto-generated method stub
                if (cnnt == UsbPrinter.STATE_CONNECTED) {
                    tSta.post(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            tSta.setText("打印机状态：已连接");
                        }
                    });

                    tId.post(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            long id = usbPrinter.GetSerialNum();
                            tId.setText("打印机ID：" + String.format("0x%X", id));
                        }
                    });
                } else {
                    tSta.post(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            tSta.setText("打印机状态：未连接");
                        }
                    });

                    tId.post(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            long id = -1;
                            tId.setText("打印机ID：" + String.format("0x%X", id));
                        }
                    });
                }
            }
        };
        //打印机纸状态
        OnStatusListener mOnStatusListener = new OnStatusListener() {
            @Override
            public void onStatus(int cnnt) {
                Log.i(TAG, "onStatus: 打印机状态 = " + cnnt);
                // TODO Auto-generated method stub
                if (cnnt == UsbPrinter.STATE_NORMAL) {
                    if (tv_Sta != null) {
                        tv_Sta.post(new Runnable() {
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                tv_Sta.setText("实时状态：有纸");
                            }
                        });
                    }
                } else if (cnnt == UsbPrinter.STATE_PE) {
                    if (tv_Sta != null) {
                        tv_Sta.post(new Runnable() {
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                tv_Sta.setText("实时状态：缺纸");
                            }
                        });
                    }
                } else {
                    if (tv_Sta != null) {
                        tv_Sta.post(new Runnable() {
                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                tv_Sta.setText("实时状态：未连接");
                            }
                        });
                    }
                }
            }
        };

        if (usbPrinter == null) {
            usbPrinter = new UsbPrinter();

            //usbPrinter.PrinterModel = 2; //支持另一个机型

            usbPrinter.setOnConnectListener(mOnConnectListener);
            usbPrinter.setOnStatusListener(mOnStatusListener);

            usbPrinter.init(this.getApplication());

            tSta.setText("打印机状态：未连接");
            tv_Sta.setText("实时状态：未连接");
            tId.setText("打印机ID：" + String.format("0x%X", -1l));
        }

        //防止Focus在EditText上显示软键盘
        RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.main_layout);
        myLayout.setFocusable(true);
        myLayout.setFocusableInTouchMode(true);
        myLayout.requestFocus();

        //设置字体放大倍数
        EditText etH = (EditText) findViewById(R.id.eTextHor);
        EditText etV = (EditText) findViewById(R.id.eTextVert);

        etH.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                String str = s.toString();
                if (!str.isEmpty()) {
                    int rate = Integer.parseInt(str);
                    if (rate <= 8) {
                        int len = usbPrinter.POS_SetFont((byte) 0, (byte) rate, (byte) 0);
                        if (len < 0) {
                            Toast.makeText(MainActivity.this, "横向放大设置失败! ", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    } else {
                        Toast.makeText(MainActivity.this, "设置值不能大于8! ", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
        etV.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                String str = s.toString();
                if (!str.isEmpty()) {
                    int rate = Integer.parseInt(str);
                    if (rate <= 8) {
                        int len = usbPrinter.POS_SetFont((byte) 0, (byte) 0, (byte) rate);
                        if (len < 0) {
                            Toast.makeText(MainActivity.this, "纵向放大设置失败! ", Toast.LENGTH_SHORT).show();
                            return;
                        }
                    } else {
                        Toast.makeText(MainActivity.this, "设置值不能大于8! ", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        //选择打印方向
        Spinner mPrintDir = (Spinner) findViewById(R.id.spnPrintDir);
        List<String> PrintDir = new ArrayList<String>();
        PrintDir.add("正向打印");
        PrintDir.add("反向打印");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, PrintDir);
        mPrintDir.setAdapter(adapter);

        dirSelected = mPrintDir.getSelectedItemPosition();

        mPrintDir.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //防止程序启动时
                if (dirSelected == position) {
                    return;
                }
                // TODO Auto-generated method stub
                if (0 == position) {
                    int len = usbPrinter.POS_SetPrintDirection(PRINT_DIR._NORMAL);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "设置正向打印失败! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    dirSelected = position;
                } else {
                    int len = usbPrinter.POS_SetPrintDirection(PRINT_DIR._REVERSE);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "设置反向打印失败! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    dirSelected = position;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }
        });

        //字符串打印
        Button btnString = (Button) findViewById(R.id.btnSTR);
        btnString.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String str = "北京迅普科技有限公司打印测试程序";
                int len = usbPrinter.POS_PrintString(str);
                if (len < 0) {
                    Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
        //1024行字符压力测试，32K字符，共34K数据
        Button btn = (Button) findViewById(R.id.btnSend);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int cnt;
                for (cnt = 0; cnt < 1024; cnt++) {
                    String str = "abcdefghijklmnopqrstuvwxyz"; //26byte
                    str += String.format("%06d", cnt); //26+6

                    byte[] strByte = str.getBytes();
                    byte[] szSend = new byte[strByte.length + 2];
                    int nSize = strByte.length + 2;        //26+6+2

                    for (int i = 0; i < strByte.length; i++) {
                        szSend[i] = strByte[i];
                    }
                    szSend[strByte.length] = 0x0d;
                    szSend[strByte.length + 1] = 0x0a;

                    int len = usbPrinter.WriteUsb(szSend, nSize);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! " + cnt, Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
            }
        });
        //图形打印
        Button btn1B4B = (Button) findViewById(R.id.btn1B4B);
        btn1B4B.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                byte[] PictureData = {0x7c, 0x44, 0x44, (byte) 0xff, 0x44, 0x44, 0x7c, 0x00, 0x41, 0x62, 0x54, (byte) 0xc8, 0x54, 0x62, 0x41};
                try {
                    int len = usbPrinter.POS_PrintPicture(15, PictureData);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    len = usbPrinter.POS_Print();
                    len = usbPrinter.POS_Print();
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //图形打印
        Button btnBMP = (Button) findViewById(R.id.btnBMP);
        btnBMP.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    int len;
                    Resources res = getResources();

                    Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.timg);

                    //Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.test_24);

                    usbPrinter.POS_SetAlign(ALIGN._MIDDLE);

                    usbPrinter.POS_PrintBitmap(bmp);
                    len = usbPrinter.POS_Print();
                    len = usbPrinter.POS_Print();
                    len = usbPrinter.POS_Print();
                    len = usbPrinter.POS_Print();

                    //usbPrinter.POS_PrintBitmapX(bmp, 0);
                    //len = usbPrinter.POS_Print();
                    //len = usbPrinter.POS_Print();

                    //usbPrinter.POS_PrintBitmapX(bmp, 1);
                    //len = usbPrinter.POS_Print();
                    //len = usbPrinter.POS_Print();

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        //光栅位图打印
        Button btn1D76 = (Button) findViewById(R.id.btn1D76);
        btn1D76.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int[] Pic1D76_58mm = {/*29,118,48,0,60,0,40,0,*/0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 6, 0, 0, 12, 0, 0, 0, 0, 28, 1,
                        128, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0, 3, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 195, 192,
                        0, 0, 15, 0, 0, 30, 48, 0, 0, 0, 30, 3, 224, 0, 0, 192,
                        15, 0, 14, 0, 60, 0, 0, 3, 192, 0, 0, 0, 0, 0, 0, 0,
                        3, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 192, 0, 0, 15, 0,
                        0, 30, 63, 255, 224, 0, 31, 3, 192, 0, 15, 224, 15, 0, 14, 0,
                        60, 0, 0, 7, 128, 0, 15, 254, 127, 254, 0, 3, 195, 192, 0, 127,
                        255, 255, 248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 1, 195, 192, 0, 0, 7, 128, 0, 30, 63, 255,
                        224, 0, 15, 7, 128, 1, 255, 224, 15, 0, 14, 0, 60, 0, 0, 7,
                        128, 0, 7, 254, 127, 254, 0, 3, 193, 224, 0, 127, 255, 255, 240, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 1, 195, 192, 1, 128, 7, 128, 0, 15, 1, 192, 224, 7, 255, 255,
                        255, 3, 254, 7, 15, 0, 14, 0, 60, 0, 0, 7, 128, 0, 7, 28,
                        112, 14, 0, 3, 193, 224, 0, 0, 0, 0, 240, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 192,
                        1, 255, 255, 255, 252, 15, 1, 192, 224, 7, 255, 255, 255, 3, 222, 7,
                        143, 0, 14, 0, 60, 0, 63, 255, 255, 255, 7, 28, 112, 14, 0, 7,
                        128, 240, 0, 0, 0, 0, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 192, 1, 255, 255, 255,
                        252, 6, 1, 192, 224, 7, 231, 255, 255, 0, 30, 3, 207, 0, 14, 31,
                        255, 248, 63, 255, 255, 255, 7, 60, 112, 14, 0, 7, 128, 240, 0, 0,
                        0, 0, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 1, 195, 192, 0, 0, 0, 0, 0, 0, 1, 192,
                        224, 0, 199, 142, 24, 0, 30, 1, 239, 1, 206, 255, 255, 248, 0, 15,
                        0, 0, 7, 56, 127, 254, 0, 15, 0, 120, 0, 0, 0, 0, 240, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 1, 195, 192, 192, 0, 0, 0, 0, 0, 1, 192, 224, 1, 231, 142,
                        60, 0, 30, 1, 255, 1, 255, 224, 60, 0, 0, 30, 0, 0, 7, 56,
                        127, 254, 0, 30, 0, 120, 1, 255, 255, 252, 240, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 193,
                        224, 0, 0, 0, 0, 0, 1, 192, 224, 1, 231, 142, 60, 0, 30, 0,
                        255, 1, 255, 224, 60, 0, 0, 60, 0, 0, 7, 120, 112, 14, 0, 30,
                        0, 60, 1, 255, 255, 252, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 199, 240, 15, 255, 255,
                        128, 0, 1, 192, 224, 0, 247, 142, 120, 7, 255, 224, 15, 0, 14, 0,
                        60, 0, 0, 60, 0, 0, 7, 120, 112, 14, 0, 60, 0, 62, 1, 255,
                        255, 252, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 3, 255, 195, 207, 128, 15, 255, 255, 128, 0, 1, 192,
                        224, 0, 127, 142, 112, 7, 255, 224, 15, 0, 14, 0, 60, 0, 0, 127,
                        255, 248, 7, 112, 112, 14, 0, 124, 0, 31, 0, 0, 0, 0, 240, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        3, 255, 195, 255, 0, 15, 0, 7, 128, 255, 63, 254, 224, 0, 103, 142,
                        112, 7, 255, 238, 15, 0, 14, 0, 60, 0, 0, 255, 255, 248, 7, 240,
                        112, 14, 0, 120, 0, 15, 0, 0, 0, 0, 240, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 252,
                        0, 15, 0, 7, 128, 255, 63, 254, 224, 31, 135, 142, 15, 192, 62, 15,
                        15, 0, 14, 127, 255, 240, 1, 240, 0, 120, 7, 120, 127, 254, 0, 240,
                        120, 7, 192, 0, 0, 0, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 240, 0, 15, 0, 7,
                        128, 7, 1, 192, 224, 31, 255, 255, 255, 192, 62, 7, 143, 0, 15, 255,
                        255, 240, 3, 240, 0, 120, 7, 60, 127, 254, 1, 224, 120, 7, 224, 0,
                        0, 0, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 1, 195, 192, 0, 15, 0, 7, 128, 7, 1, 192,
                        224, 31, 255, 255, 255, 192, 63, 195, 207, 0, 15, 231, 128, 240, 7, 240,
                        0, 120, 7, 28, 115, 128, 3, 224, 120, 3, 240, 63, 255, 240, 240, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 1, 195, 192, 0, 15, 0, 7, 128, 7, 1, 192, 224, 0, 0, 0,
                        0, 0, 127, 225, 239, 0, 31, 131, 128, 224, 15, 255, 255, 248, 7, 30,
                        115, 128, 7, 192, 240, 1, 224, 63, 255, 240, 240, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 192,
                        0, 15, 255, 255, 128, 7, 1, 192, 224, 0, 0, 0, 0, 0, 126, 240,
                        239, 0, 126, 3, 129, 224, 31, 127, 255, 248, 7, 30, 115, 199, 3, 128,
                        240, 0, 0, 28, 0, 240, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 192, 0, 15, 255, 255,
                        128, 7, 1, 192, 224, 1, 255, 255, 252, 0, 254, 248, 207, 115, 254, 3,
                        193, 192, 62, 127, 255, 248, 7, 14, 113, 207, 128, 1, 224, 0, 0, 28,
                        0, 240, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 1, 195, 192, 0, 0, 7, 128, 0, 7, 1, 192,
                        240, 1, 255, 255, 252, 0, 254, 96, 31, 249, 238, 1, 195, 192, 124, 112,
                        0, 120, 7, 30, 113, 222, 0, 1, 192, 0, 0, 28, 0, 240, 240, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 1, 195, 192, 0, 1, 135, 128, 0, 7, 1, 192, 246, 1, 224, 0,
                        60, 1, 254, 3, 255, 224, 14, 1, 227, 128, 56, 112, 0, 120, 7, 30,
                        113, 248, 0, 3, 192, 240, 0, 28, 0, 240, 240, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 192,
                        0, 3, 135, 140, 0, 7, 1, 192, 119, 129, 224, 0, 60, 1, 222, 127,
                        255, 0, 14, 1, 231, 128, 0, 112, 0, 120, 7, 62, 112, 240, 0, 3,
                        128, 240, 0, 28, 0, 240, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 195, 192, 0, 7, 199, 159,
                        0, 7, 1, 192, 127, 1, 224, 0, 60, 3, 222, 127, 143, 0, 14, 0,
                        255, 0, 0, 127, 255, 248, 7, 252, 112, 240, 0, 7, 128, 120, 0, 31,
                        255, 240, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 7, 195, 192, 0, 15, 135, 143, 128, 7, 1, 192,
                        63, 0, 255, 255, 252, 7, 158, 56, 15, 0, 14, 0, 127, 0, 0, 127,
                        255, 248, 7, 120, 112, 120, 0, 15, 0, 60, 0, 31, 255, 240, 240, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 127, 195, 192, 96, 31, 7, 135, 192, 7, 1, 192, 62, 0, 255, 255,
                        252, 7, 158, 0, 15, 0, 14, 0, 126, 0, 0, 112, 0, 120, 7, 96,
                        112, 120, 0, 15, 0, 60, 0, 28, 0, 240, 240, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 255, 195, 192,
                        248, 62, 7, 131, 224, 15, 129, 192, 30, 0, 224, 0, 60, 3, 30, 0,
                        15, 0, 14, 0, 126, 0, 0, 112, 0, 120, 7, 0, 119, 60, 0, 30,
                        1, 254, 0, 28, 0, 240, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 7, 241, 195, 192, 240, 124, 7, 129,
                        248, 31, 192, 0, 0, 0, 224, 0, 60, 0, 30, 0, 15, 0, 14, 1,
                        255, 0, 0, 112, 0, 120, 7, 0, 127, 31, 0, 63, 255, 255, 0, 60,
                        0, 0, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 3, 129, 195, 225, 240, 248, 7, 128, 248, 61, 252, 0,
                        0, 1, 224, 0, 60, 0, 30, 0, 15, 0, 30, 7, 231, 192, 0, 112,
                        0, 120, 7, 0, 255, 15, 192, 127, 255, 207, 0, 60, 0, 0, 240, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 1, 193, 255, 224, 240, 15, 128, 112, 120, 255, 255, 255, 1, 255, 255,
                        252, 0, 30, 0, 15, 0, 254, 63, 195, 248, 0, 112, 3, 248, 7, 0,
                        252, 7, 192, 127, 224, 15, 128, 0, 0, 15, 240, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 192, 255,
                        192, 96, 127, 0, 0, 120, 63, 255, 254, 1, 255, 255, 252, 0, 30, 0,
                        15, 0, 254, 127, 0, 252, 0, 112, 3, 240, 15, 0, 240, 3, 128, 56,
                        0, 7, 128, 0, 0, 15, 240, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 192, 0, 0, 0, 62, 0,
                        0, 48, 1, 255, 254, 1, 224, 0, 60, 0, 30, 0, 15, 0, 120, 56,
                        0, 60, 0, 112, 3, 224, 15, 0, 96, 0, 0, 0, 0, 3, 0, 0,
                        0, 7, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 12, 24, 56, 56, 56, 56, 56, 56, 56, 56, 56, 48,
                        112, 112, 112, 112, 112, 112, 112, 112, 96, 224, 224, 224, 224, 224, 224, 224,
                        224, 224, 193, 193, 193, 193, 193, 193, 193, 193, 193, 131, 131, 131, 130, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        3, 102, 198, 198, 198, 198, 198, 198, 198, 198, 198, 205, 141, 141, 141, 141,
                        141, 141, 141, 141, 155, 27, 27, 27, 27, 27, 27, 27, 27, 27, 54, 54,
                        54, 54, 54, 54, 54, 54, 54, 108, 108, 108, 108, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 129, 1, 1,
                        1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8,
                        8, 8, 8, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0};
                try {
                    byte[] PictureData = new byte[Pic1D76_58mm.length];
                    for (int i = 0; i < Pic1D76_58mm.length; i++) {
                        PictureData[i] = (byte) Pic1D76_58mm[i];
                    }
                    int len = usbPrinter.POS_PrintGratingPicture(60, 40, PictureData);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    len = usbPrinter.POS_Print();
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //条码打印
        Button btnBar = (Button) findViewById(R.id.btnBAR);
        btnBar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int len = 0;
                try {
                    len = usbPrinter.POS_SetBarcodeHRI(HRI._BOTTOM);
                    if (len < 0) {

                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    len = usbPrinter.POS_SetAlign(ALIGN._MIDDLE);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    String str1 = "12345ABCDE";
                    len = usbPrinter.POS_Print1DBarcode(BAR_TYPE_1D._CODE128, str1.getBytes("GBK"));
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    len = usbPrinter.POS_PrintString(str1);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        //条码打印
        Button btnBar13 = (Button) findViewById(R.id.btnBar13);
        btnBar13.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int len = 0;
                try {
                    len = usbPrinter.POS_SetBarcodeHRI(HRI._BOTTOM);
                    if (len < 0) {

                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    len = usbPrinter.POS_SetAlign(ALIGN._MIDDLE);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    String str1 = "123456789012";
                    len = usbPrinter.POS_Print1DBarcode(BAR_TYPE_1D._EAN13, str1.getBytes("GBK"));
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    len = usbPrinter.POS_PrintString(str1);
                    if (len < 0) {
                        Toast.makeText(MainActivity.this, "Send Failed! ", Toast.LENGTH_SHORT).show();
                        return;
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        //=======================================
        Button btn1C26 = (Button) findViewById(R.id.btn1C26);
        btn1C26.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetCharMode(TYPE_CHAR._HZ);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1C2E = (Button) findViewById(R.id.btn1C2E);
        btn1C2E.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetCharMode(TYPE_CHAR._CHAR);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B4A = (Button) findViewById(R.id.btn1B4A);
        btn1B4A.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_PrintAndFeedNDotLine(FEED_DIR._NORMAL, (byte) 160);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B64 = (Button) findViewById(R.id.btn1B64);
        btn1B64.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_PrintAndFeedNLine(FEED_DIR._NORMAL, (byte) 3);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B660 = (Button) findViewById(R.id.btn1B660);
        btn1B660.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_PrintSpaceOrSpaceLine(TYPE_SPACE._SPACE, (byte) 3);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B661 = (Button) findViewById(R.id.btn1B661);
        btn1B661.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_PrintSpaceOrSpaceLine(TYPE_SPACE._LINE, (byte) 3);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1D42 = (Button) findViewById(R.id.btn1D42);
        btn1D42.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetFontReverse(REVERSE_VIDEO._ENABLE);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B45 = (Button) findViewById(R.id.btn1B45);
        btn1B45.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetFontBold(FONT_BOLD._ENABLE);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B2D = (Button) findViewById(R.id.btn1B2D);
        btn1B2D.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetUnderLine(FONT_UNDERLINE._ENABLE);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B610 = (Button) findViewById(R.id.btn1B610);
        btn1B610.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetAlign(ALIGN._LEFT);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B611 = (Button) findViewById(R.id.btn1B611);
        btn1B611.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetAlign(ALIGN._MIDDLE);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B612 = (Button) findViewById(R.id.btn1B612);
        btn1B612.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetAlign(ALIGN._RIGHT);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B32 = (Button) findViewById(R.id.btn1B32);
        btn1B32.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetDefaultLineSpace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B31 = (Button) findViewById(R.id.btn1B31);
        btn1B31.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetLineSpace((byte) 80);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B6C = (Button) findViewById(R.id.btn1B6C);
        btn1B6C.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetMargin((byte) 3, (byte) 0);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B51 = (Button) findViewById(R.id.btn1B51);
        btn1B51.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_SetMargin((byte) 0, (byte) 3);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1B40 = (Button) findViewById(R.id.btn1B40);
        btn1B40.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_Init();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        final Button btn1C49 = (Button) findViewById(R.id.btn1C49);
        btn1C49.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    bRotate++;
                    bRotate &= 0x03;
                    if (0 == bRotate) {
                        usbPrinter.POS_SetFontRotate(FONT_ROTATE._Rotate0);
                        btn1C49.setText("字符旋转0度");
                    } else if (1 == bRotate) {
                        usbPrinter.POS_SetFontRotate(FONT_ROTATE._Rotate90);
                        btn1C49.setText("字符旋转90度");
                    } else if (2 == bRotate) {
                        usbPrinter.POS_SetFontRotate(FONT_ROTATE._Rotate180);
                        btn1C49.setText("字符旋转180度");
                    } else if (3 == bRotate) {
                        usbPrinter.POS_SetFontRotate(FONT_ROTATE._Rotate270);
                        btn1C49.setText("字符旋转270度");
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        //=======================================
        Button btn1D56 = (Button) findViewById(R.id.btn1D56);
        btn1D56.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    usbPrinter.POS_CutPaper(CUT_MODE.TYPE_FEED2CUT, (byte) 0);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });


        Button btnNVBMP = (Button) findViewById(R.id.btnNVBMP);
        btnNVBMP.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                usbPrinter.POS_PrintNVBMP((byte) 1);
                Toast.makeText(MainActivity.this, "打印NV位图01", Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        usbPrinter = null;

        System.exit(0);

        super.onDestroy();
    }
}
